function onload(){
    var type = getQueryString();
    initBlogList("1",type);
    initRecommendList();
}
function initInfo() {
    getBlogContext();
    initRecommendList();
}
function page(pageNum,type){
    initBlogList(pageNum,type);
}
function getQueryString() {
    var reg = new RegExp("(^|&)type=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return "";
}
function initRecommendList() {
    $.ajax({
        url: "/chengqj/blog/queryRecommendAll",
        method: 'GET',
        async: false,
        success: function (data) {
            var datas = data;
            if (datas.length === 0) {
                return;
            }
            var blogList = "<ul>";
            for (var i = 0; i < datas.length; i++) {
                var title = datas[i].title;
                var contextUrl = "/info.html?id="+ datas[i].blogId;
                blogList = blogList + "<li><a href=\""+ contextUrl + "\">"+ title + "</a></li>";
            }
            blogList = blogList + "</ul>";
            $("#tuijian").append(blogList);
        },
        error: function (data) {
            alert("hehe");
        }
    });
}

function initBlogList(pageNum,type){
    $("main").empty();
    var pageSize = "10";
    var paramer = "?type=" + type + "&pageNum=" + pageNum + "&pageSize=" + pageSize;
    $.ajax({
        url: "/chengqj/blog/queryBlogInfoList"+paramer,
        method: 'GET',
        async: false,
        success: function (data) {
            var datas = data.list;
            if (datas.length === 0) {
                return;
            }
            //文章列表
            var blogList = "";
            for (var i = 0; i < datas.length; i++) {
                var title = datas[i].title;
                var img = datas[i].imgUrl;
                var contextUrl = "/info.html?id="+ datas[i].id;
                blogList = blogList + "<li><i><a href=\"/\"><img src=\""+img +"\"></a></i>";
                blogList = blogList + "<h3><a href=\""+ contextUrl + "\">"+title +"</a></h3>";
                blogList = blogList + "<p>"+datas[i].preface +"</p> </li>";
            }
            $("main").prepend(blogList);
            //页码
            var pageNum = data.pageNum;
            var total = data.total;
            var perPage = pageNum>0?pageNum:pageNum-1;
            var nextPage =data.nextPage
            var hasPreviousPage = data.hasPreviousPage;
            var hasNextPage = data.hasNextPage;
            var pageList = "<div class=\"pagelist\"><a class=\"allpage\"><b>"+ total + "</b></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            if(hasPreviousPage){
                pageList = pageList + "<a href=\"#\" onclick=\"page(" + perPage +",'"+ type + "')\">前一页</a>&nbsp;&nbsp;";
            }
            for (var j = 0;j<data.navigatepageNums.length;j++){
                var pageValue = data.navigatepageNums[j];
                if (pageNum == pageValue){
                    pageList = pageList + "<a href=\"#\" class=\"curPage\">" + pageNum + "</a>&nbsp;&nbsp;";
                }else{
                    pageList = pageList + "<a href=\"#\" onclick=\"page("+ pageValue + ",'"+ type + "')\">" + pageValue + "</a>&nbsp;&nbsp;" ;
                }
            }
            if (hasNextPage) {
                pageList = pageList + "<a href=\"#\" onclick=\"page("+ nextPage + ",'"+ type + "')\">后一页</a></div>";
            }
            $("main").append(pageList);
        },
        error: function (data) {
            alert("hehe");
        }
    });
}

function getBlogContext()
{
    var reg = new RegExp("(^|&)id=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    var id = unescape(r[2]);
    if(id ==null || id.toString().length<1)
    {
        return;
    }
    $.ajax({
        url: "/chengqj/blog/queryBlogInfoById/"+id,
        method: 'GET',
        async: false,
        success: function (data) {
            var datas = data;
            //文章内容
            $("#newsview").prepend(datas);
        },
        error: function (data) {
            alert("hehe");
        }
    });
}
